﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RTW.OA.Areas.Personal.Servsers;
using RTW.OA.Models;
using RTW.OA.Areas.Personal.Models;//.AddressViewModel
using RTW.Utils.Common;

namespace RTW.OA.Areas.Personal.Controllers
{
    public class AddressController : Controller
    {
        private AddressServers addressservers;
        private AddressGroupServers addressgroupservers;

        public AddressController()
        {
            addressservers = new AddressServers();
            addressgroupservers = new AddressGroupServers();
        }
        
        public ActionResult Index()
        {
            var model = new AddressViewModel
            {
                AddressGroupLists = addressgroupservers.GetGroupModel()
            };
            return View(model);
        }

        public ActionResult GroupList(string userId, string groupId, string keyWord)
        {
            var model = new AddressViewModel
            {
                Address_Group = addressgroupservers.GetModelById(groupId),
                AddressLists = addressservers.GetModelByGroupId(groupId, keyWord)
            };
            return View(model);
        }

        public ActionResult Details(string id, string groupId, string keyWord)
        {
            address model = new address();
            model = addressservers.GetModelById(id, groupId, keyWord);
            return View(model);
        }
        
        public ActionResult AddressGroup()
        {
            ViewBag.Title = "通讯录分组";
            var model = new AddressViewModel
            {
                AddressGroupLists = addressgroupservers.GetGroupModel()
            };
            return View(model);
        }
        
        public ActionResult ShowGroup(string groupId)
        {
            string AddressList = JsonHelper.ObjectToJSON(addressservers.GetModelByGroupId("sg", null));
            ViewBag.AddressList = AddressList;
            var model = new AddressViewModel
            {
                Address_Group = addressgroupservers.GetModelById(groupId)
            };
            return View(model);
        }
        
        public ActionResult AddressEdit(int? id)
        {
            ViewBag.GROUP_IDData = addressservers.GROUP_IDData();
            var model = new address();
            if (id != null)
            {
                ViewBag.Title = "编辑通讯录";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建通讯录";
            }
            return View(model);
        }
        
        [HttpPost]
        public ActionResult AddressEdit(int? id, address model)
        {
            ViewBag.GROUP_IDData = addressservers.GROUP_IDData();
            try
            {
                if (id == null || id == 0)
                {
                    addressservers.Add(model);
                    ViewBag.msg = "新增通讯录信息成功！";
                    model = new address();
                }
                else
                {
                    addressservers.Update(model);
                    ViewBag.msg = "修改通讯录信息成功！";

                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        public ActionResult AddressGroupEdit(int? id)
        {
            var model = new address_group();
            if (id != null)
            {
                ViewBag.Title = "编辑分组";
                model = GetGroupModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建分组";
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult AddressGroupEdit(int? id, address_group model)
        {
            try
            {
                if (id == null || id == 0)
                {
                    addressgroupservers.Add(model);
                    ViewBag.msg = "新增分组信息成功！";
                    model = new address_group();
                }
                else
                {
                    addressgroupservers.Update(model);
                    ViewBag.msg = "修改分组信息成功！";

                }
            }
            catch (Exception ex)
            {
                ViewBag.msg = "提交出现错误:" + ex.ToString();
            }
            
            return RedirectToAction("AddressGroup"); 
        }
        
        public ActionResult AddressGroupDelete(int id)
        {
            try
            {
                if (id != 0)
                {
                    addressgroupservers.Del(id.ToString());
                    ViewBag.msg = "删除分组信息成功！";
                }
                else
                {
                    ViewBag.msg = "删除分组信息失败！";
                }
            }
            catch (Exception ex)
            {
                ViewBag.msg = "提交出现错误(" + ex.ToString() + ")！";
            }
            return Content("");
        }

        [HttpPost]
        public ActionResult AddressGroupReEdit(string choosedIdArr, string groupId, string groupName, string userId)
        {
            try
            {
                int groupIdint = Convert.ToInt32(groupId);
                addressgroupservers.UpdateGroupNameById(groupIdint, groupName);
                string[] choose = choosedIdArr.Split('|');
                string[] choosed = choose[0].Split(',');//已选成员
                string[] unChoosed = choose[1].Split(',');//备选成员
                
                if (choosed.Count() > 0)
                {
                    foreach (var item in choosed)
                    {
                        int addId = 0;
                        if (!string.IsNullOrEmpty(item))
                        {
                            addId = Convert.ToInt32(item);
                            addressservers.UpdateGroupById(addId, groupIdint);
                        }
                    }
                }
                if (unChoosed.Count() > 0)
                {
                    foreach (var item in unChoosed)
                    {
                        int addId = 0;
                        if (!string.IsNullOrEmpty(item))
                        {
                            addId = Convert.ToInt32(item);
                            addressservers.UpdateGroupById(addId, null);
                        }
                    }
                }
                ViewBag.msg = "保存成功!";
            }
            catch(Exception ex)
            {
                ViewBag.msg = "提交出现错误:" + ex.ToString();
            }
            return Content("");
        }

        #region //GetData


        public address GetModel(string id)
        {
            address model = new address();
            model = addressservers.GetModelById(id, "", "");
            return model;
        }

        public address_group GetGroupModel(string id)
        {
            var model = addressgroupservers.GetModelById(id);
            return model;
        }

        #endregion //GetData
    }
}
